<cfcomponent displayname="Application">
	<cfscript>
		this.name = "wikifusion_#right(hash(GetDirectoryFromPath(GetCurrentTemplatePath())),20)#_v1";
		this.setclientcookies="yes";
		this.sessionmanagement="yes";
		this.sessiontimeout= CreateTimeSpan(0,0,60,0);
		this.mappings['/com/wikifusion'] = GetDirectoryFromPath(GetCurrentTemplatePath()) & "components";
	</cfscript>
		
	<cffunction name="OnApplicationStart" output="false" access="public" returntype="boolean" hint="Fires when the application is started.">		
		<cftry>
			<cfset Application.wikiOBJ = createobject("component", "com.wikifusion.wikiSettings").init(applicationname=Application.applicationname) />
			<cfset Application.datasourceOBJ = Application.wikiOBJ.getWikiDatabaseObj(GetDirectoryFromPath(GetCurrentTemplatePath()) & "config.ini") />
			<cfset Application.wikiOBJ.loadWikiData()>
			<cfcatch type="MissingConfigFile">
				<cflocation URL="admin/setup/setup.cfm" addtoken="false"> 
			</cfcatch>
		</cftry>

		<cfset Application.sessions = structNew()>
		 
		<cfreturn true>
	</cffunction>	
	
	<cffunction name="OnApplicationEnd" access="public" returntype="void" output="false" hint="Fires when the application is terminated.">
		<cfargument name="ApplicationScope" type="struct" required="false" default="#StructNew()#" />
		
		<cfreturn />
	</cffunction>

	<cffunction name="OnSessionStart" access="public" returntype="boolean" output="false" hint="Fires when the session is first created.">
			<cfset Application.sessions[session.urltoken] = 1>
		 	<cfset Session.user = createObject("component", "com.wikifusion.user").init(datasourceOBJ=Application.datasourceOBJ) />	 
		
		<cfreturn true>
	</cffunction>
	
	<cffunction name="OnSessionEnd" access="public" returntype="void" output="false" hint="Fires when the session is terminated.">
		<cfargument name="SessionScope" type="struct" required="true" />
		<cfargument name="ApplicationScope" type="struct" required="false" default="#StructNew()#" />
		
		<cfset structDelete(arguments.ApplicationScope.sessions, arguments.sessionScope.urltoken)>
		<cfset StructClear(SessionScope) />
		
		<cfreturn />
	</cffunction>
	
	<cffunction name="onRequestStart" access="public" returnType="boolean" output="true">
		<cfargument type="String" name="targetPage" required="true" />		
			
		<cfif structkeyexists(url, "reinit") and url.reinit>
			<cfset OnApplicationStart() />
			<cfset OnSessionStart() />
		</cfif>		
			
	<!--- 	<cftry>
				<cfset request.blogManager = application.blogFacade.getMango()/>
				<cfcatch type="database">
					<cfset StructDelete(this,"onError") />
					<cfset request.message = createObject("component",variables.componentsPath & "Message") />
					<cfset request.message.setTitle("Database error: " & cfcatch.Detail) />
					<cfset request.message.setText(cfcatch.Message) />
					<cfset request.message.setStatus("error") />
				
					<cfheader statuscode="500" statustext="Internal Server Error"/>
					<cfsetting enablecfoutputonly="false">
					<cfinclude template="error.cfm">
					<cfabort>
					</cfcatch>
			</cftry>
			<cfif structkeyexists(url,"logout")>
				<cfset request.blogManager.getAuthorizer().unauthorize() />
			</cfif>
			
			<cfif structkeyexists(form,"login")>
				<cfset isAuthor = request.blogManager.getAuthorizer().authorize(form.username,form.password) />
				<cfif NOT isAuthor>
					<cfset request.errormsg = "Invalid login, please try again." />
					<cfset request.username = form.username />
				</cfif>
			</cfif>
			
			<cfsetting showdebugoutput="false">
			
			<cfset blog = request.blogManager.getBlog() />
			<cfset isAdmin = findNoCase(blog.getSetting('urls').admin,arguments.targetPage)/>
			<cfif isAdmin AND NOT ( request.blogManager.isCurrentUserLoggedIn() AND
					listFind(request.blogManager.getCurrentUser().getCurrentrole(blog.getId()).permissions, "access_admin") )>
				<cfset request.administrator = request.blogManager.getAdministrator() />
				<cfinclude template="admin/login.cfm">
				<cfreturn false>
			<cfelseif isAdmin>
				<!--- until we get error handling in the admin, remove onError method --->
				<cfset StructDelete(this,"onError") />
				<cfset request.administrator = request.blogManager.getAdministrator() />
				<cfset request.formHandler = createObject("component","admin.FormHandler").init(request.administrator, 
					request.blogManager.getCurrentUser()) />
			</cfif>
			
			<cfset structappend(request,request.blogManager.handleRequest(targetPage,url,form),true)/>
			 --->
		<cfreturn true>
	</cffunction>	
	
	<cffunction name="OnRequestEnd" access="public" returntype="void" output="true" hint="Fires after the page processing is complete.">
		
		<cfreturn />
	</cffunction>
	
<!--- 	<cffunction name="onError" returnType="void">
		<cfargument name="Exception" required="true" />
		<cfargument name="EventName" type="String" required="true"/

		<!--- <cfset request.message = createObject("component",variables.componentsPath & "Message") />
		<cfset request.message.setTitle("Error: " & arguments.EventName) />
		<cfset request.message.setText(arguments.exception.message) />
		<cfset request.message.setStatus("error") />
		<cfset request.message.setData(arguments.exception.detail) />
		
		<!--- Set appropriate HTTP status code --->
		<cfif REFindNoCase("NotFound$",arguments.exception.type)>
			<cfheader statuscode="404" statustext="Not Found"/>
		<cfelse>
			<cfheader statuscode="500" statustext="Internal Server Error"/>
		</cfif>
		
		<cftry>
			<cfsetting enablecfoutputonly="false">
			<cfinclude template="generic.cfm">
		<cfcatch type="any">
			<cfsetting enablecfoutputonly="false">
			<cfinclude template="error.cfm">
		</cfcatch>
		</cftry> --->
	</cffunction> --->
	
</cfcomponent>